package com.zhongli.devplatform.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongli.devplatform.bean.Res;
import com.zhongli.devplatform.bean.SecurityHelper;
import com.zhongli.devplatform.po.sys.ReadChapterExercisesPO;
import com.zhongli.devplatform.po.sys.ReadKnowledgePointPO;
import com.zhongli.devplatform.service.imp.sys.ReadChapterExercisesService;
import com.zhongli.devplatform.service.imp.sys.ReadKnowledgePointService;
import com.zhongli.devplatform.utils.BeanCopierUtil;
import com.zhongli.devplatform.vo.sys.ReadKnowledgePointVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wy
 * @since 2020/7/31
 */
@Api(tags = "知识点", description = "知识点")
@RestController
@RequestMapping("/knowledgePoint")
public class KnowledgePointController {

    @Autowired
    private ReadKnowledgePointService knowledgePointService;

    @Autowired
    private ReadChapterExercisesService chapterExercisesService;

    @ApiOperation("根据当前学生所选章节ID获取所有的知识点")
    @GetMapping("/getKnowledgeByChapterId/{id}")
    public Res<List<ReadKnowledgePointVO>> getKnowledgeByChapterId(@ApiParam(value = "所选章节ID",required = true) @PathVariable Integer id){
        List<ReadKnowledgePointPO> data = knowledgePointService
                .list(new QueryWrapper<ReadKnowledgePointPO>().eq("chapter_id",id));
        List<ReadKnowledgePointVO> vos = BeanCopierUtil.copy(data, ReadKnowledgePointVO.class);
        assert vos != null;
        vos.forEach(item->{
            ReadChapterExercisesPO exercisesPO = chapterExercisesService.getOne(new QueryWrapper<ReadChapterExercisesPO>()
                    .eq("student_id", SecurityHelper.getCurrentUserId())
                    .isNotNull("end_time")
                    .eq("knowledge_id", item.getId()));
            item.setIsFinish(!(exercisesPO == null));
        });
        return Res.ok(vos);
    }
}
